package edu.gmu.middleware;

import calendar.*;
import calendar.messages.*;
import edu.gmu.messages.EndPoint;
import edu.gmu.server.RmiSkeleton;
import java.io.IOException;
import java.util.ArrayList;

public class CallbackSkeleton extends RmiSkeleton implements CallbackInt
{
    private EndPoint endpoint    = null;
    
    /**
     * Creates a new CalendarSkeleton with specified params.
     */
    public CallbackSkeleton(    EndPoint endpoint,
                                String name )
    {
        super( endpoint.port, endpoint.port, name );
        
        this.endpoint = endpoint;
    }
    
    public EndPoint getEndPoint()
    {
        return this.endpoint;
    }
    
    
    /**
     * Handles a new client connection.
     * @param Socket clientSocket
     */
    public void handleRequests( RemoteStub stub ) throws IOException
    {
        //--------------------------------------------------------------------//
        // Executed in handlerThread
        //--------------------------------------------------------------------//
        stub.register(Topic.NOTIFY_EVENT,            NotifyEvent.class);
        stub.register(Topic.NOTIFY_USER,             NotifyUser.class);
        stub.register(Topic.RECONNECT_CLIENT,        ReconnectRequest.class);
        
        /*
         * Read from clientsocket continusouyly until error or disconnect 
         */
        //boolean disconnect = false;
        while( stopped == false )
        {
            Message message = stub.receive();
            
            if( message instanceof NotifyEvent )
            {
                NotifyEvent request = (NotifyEvent)message;
            }
            else if( message instanceof NotifyUser )
            {
                NotifyUser request = (NotifyUser)message;
            }
            else if( message instanceof ReconnectRequest )
            {
                ReconnectRequest request = (ReconnectRequest)message;
            }
        }
        stub.stop();
    }
    
    
    
    //------------------------------------------------------------------------//
    // Callback stuff
    //------------------------------------------------------------------------//
    public void notifyClientEvent( Event event, boolean appt ) throws IOException
    {
        NotifyEvent eventNotify = new NotifyEvent();
        eventNotify.event = event;
        eventNotify.appt  = appt;
        this.issueCallback(Topic.NOTIFY_EVENT, eventNotify);
    }

    public void notifyClientUser( User user, ArrayList users ) throws IOException
    {
        NotifyUser userNotify = new NotifyUser();
        userNotify.user  = user;
        userNotify.users = users;
        
        this.issueCallback(Topic.NOTIFY_USER, userNotify);
    }

  public void reconnect() throws IOException {
        ReconnectRequest reconnect = new ReconnectRequest();
        this.issueCallback(Topic.RECONNECT_CLIENT, reconnect);
  }
    //------------------------------------------------------------------------//
    
    

}
